package com.wwk.phone.controller;

import com.wwk.phone.entity.Book;
import com.wwk.phone.entity.vo.BookQuery;
import com.wwk.phone.exceptionhandler.MyException;
import com.wwk.phone.service.BookService;
import com.wwk.phone.utils.JwtUtils;
import com.wwk.phone.utils.R;
import com.wwk.phone.utils.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 通讯录
 *
 * @author wwk
 * @since 2023-06-05 14:59:42
 */
@RestController
@RequestMapping("friend")
@CrossOrigin
public class BookController {
    /**
     * 服务对象
     */
    @Autowired
    private BookService bookService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok().data("user", this.bookService.queryById(id));
    }

    /**
     * 通过当前用户id查询它所有的通信录好友
     */
    @GetMapping("/books")
    public R<List<Book>> queryByUserId(HttpServletRequest request) {
        if (!JwtUtils.checkToken(request)) {
            throw new MyException(ResultCode.ERROR, "token异常");
        }
        //通过jwt获取用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        return R.ok().data("list", this.bookService.queryBooksByUserId(id));
    }

    /**
     * 条件查询分页方法
     */
    @ApiOperation(value = "条件查询分页方法")
    @PostMapping("pageUserCondition/{current}/{limit}")
    public R pageUserCondition(@PathVariable("current") Long current,
                               @PathVariable("limit") Long limit,
                               @RequestBody(required = false) BookQuery bookQuery,
                               HttpServletRequest request) {
        //防止前端条件为null时，动态sql不查询shield，在xml里修复了这个bug
        if (!JwtUtils.checkToken(request)) {
            throw new MyException(ResultCode.ERROR, "非法Token");
        }
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        return bookService.pageFriendCondition(current, limit, bookQuery, userId);
    }


    /**
     * 通过friendId拉黑好友
     *
     * @param friendId
     * @return
     */
    @GetMapping("shield/{friendId}")
    public R shieldByFriendId(@PathVariable("friendId") Long friendId) {
        this.bookService.shieldFriend(friendId);
        return R.ok();
    }

    /**
     * 解禁好友
     *
     * @param friendId
     * @return
     */
    @GetMapping("unShield/{friendId}")
    public R deleteShieldById(@PathVariable("friendId") Long friendId) {
        this.bookService.recoveryFriend(friendId);
        return R.ok();
    }


    /**
     * 新增通信录数据
     *
     * @param book 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@RequestBody Book book, HttpServletRequest request) {
        if (!JwtUtils.checkToken(request)) {
            throw new MyException(ResultCode.ERROR, "非法Token");
        }
        String id = JwtUtils.getMemberIdByJwtToken(request);
        book.setUserId(id);
        boolean insert = this.bookService.insert(book);
        return insert ? R.ok() : R.error();
    }

    /**
     * 编辑数据
     *
     * @param book 实体
     * @return 编辑结果
     */
    @PutMapping
    public R<Book> edit(@RequestBody Book book, HttpServletRequest request) {
        if (!JwtUtils.checkToken(request)) {
            throw new MyException(ResultCode.ERROR, "非法Token0");
        }
        return R.ok().data("friend", this.bookService.update(book));
    }
}

